package com.jushu.storbox.database;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.database.Cursor;

import com.jushu.storbox.entities.TeamSpaceInfo;
import com.jushu.storbox.entities.TeamSpaceMemberInfo;
import com.jushu.storbox.util.LogUtil;
import com.huawei.sharedrive.sdk.android.modelV2.response.TeamSpaceMemberShipsEntity;

public class TeamSpaceMemberDao extends BaseDao implements ITeamSpaceMemberDao {
	public final static String TABLE_NAME = "tb_team_space_member_list";
	private static final String LOG_TAG = "TeamSpaceMemberDao";

	@Override
	public void insertTeamSpaceMember(
			TeamSpaceMemberShipsEntity teamSpaceMemberInfo) {
		// TODO Auto-generated method stub
		try {
			ContentValues cv = new ContentValues();
			cv.put(IBaseDao.user_id, teamSpaceMemberInfo.getMember().getId());
			cv.put(IBaseDao.memberships_id, teamSpaceMemberInfo.getId());
			cv.put(IBaseDao.teamspace_id, teamSpaceMemberInfo.getTeamspace()
					.getId());
			cv.put(IBaseDao.type, teamSpaceMemberInfo.getMember().getType());
			cv.put(IBaseDao.name, teamSpaceMemberInfo.getMember().getName());
			cv.put(IBaseDao.loginName, teamSpaceMemberInfo.getMember()
					.getLoginName());
			cv.put(IBaseDao.description, teamSpaceMemberInfo.getMember()
					.getDescription());
			cv.put(IBaseDao.team_role, teamSpaceMemberInfo.getTeamRole());
			cv.put(IBaseDao.role, teamSpaceMemberInfo.getRole());
			this.getDb().insert(TABLE_NAME, null, cv);

		} catch (Exception e) {
			e.printStackTrace();
			LogUtil.e(LOG_TAG,
					"insertTeamSpaceMember is fail:" + e.getLocalizedMessage());
		}
	}

	@Override
	public void insertTeamSpaceMember(TeamSpaceMemberInfo teamSpaceMemberInfo) {
		// TODO Auto-generated method stub
		try {
			ContentValues cv = new ContentValues();
			cv.put(IBaseDao.user_id, teamSpaceMemberInfo.getUserId());
			cv.put(IBaseDao.memberships_id,
					teamSpaceMemberInfo.getMemberShipsId());
			cv.put(IBaseDao.teamspace_id, teamSpaceMemberInfo.getTeamSpaceId());
			cv.put(IBaseDao.type, teamSpaceMemberInfo.getType());
			cv.put(IBaseDao.name, teamSpaceMemberInfo.getName());
			cv.put(IBaseDao.loginName, teamSpaceMemberInfo.getLoginName());
			cv.put(IBaseDao.description, teamSpaceMemberInfo.getDescription());
			cv.put(IBaseDao.team_role, teamSpaceMemberInfo.getTeamRole());
			cv.put(IBaseDao.role, teamSpaceMemberInfo.getRole());
			this.getDb().insert(TABLE_NAME, null, cv);

		} catch (Exception e) {
			e.printStackTrace();
			LogUtil.e(LOG_TAG,
					"insertTeamSpaceMember is fail:" + e.getLocalizedMessage());
		}
	}

	@Override
	public void updataTeamSpaceMember(
			TeamSpaceMemberShipsEntity teamSpaceMemberInfo) {
		// TODO Auto-generated method stub
		try {
			String where = teamspace_id + "=? and " + memberships_id + "=?";
			String[] whereValue = { teamSpaceMemberInfo.getTeamspace().getId(),
					teamSpaceMemberInfo.getId() };
			ContentValues cv = new ContentValues();
			cv.put(IBaseDao.user_id, teamSpaceMemberInfo.getMember().getId());
			cv.put(IBaseDao.memberships_id, teamSpaceMemberInfo.getId());
			cv.put(IBaseDao.teamspace_id, teamSpaceMemberInfo.getTeamspace()
					.getId());
			cv.put(IBaseDao.type, teamSpaceMemberInfo.getMember().getType());
			cv.put(IBaseDao.name, teamSpaceMemberInfo.getMember().getName());
			cv.put(IBaseDao.loginName, teamSpaceMemberInfo.getMember()
					.getLoginName());
			cv.put(IBaseDao.description, teamSpaceMemberInfo.getMember()
					.getDescription());
			cv.put(IBaseDao.team_role, teamSpaceMemberInfo.getTeamRole());
			cv.put(IBaseDao.role, teamSpaceMemberInfo.getRole());
			this.getDb().update(TABLE_NAME, cv, where, whereValue);

		} catch (Exception e) {
			e.printStackTrace();
			LogUtil.e(LOG_TAG,
					"updateTeamSpaceMember is fail:" + e.getLocalizedMessage());
		}
	}

	@Override
	public List<TeamSpaceMemberInfo> getTeamSpaceMemberList(String teamSpaceId) {
		// TODO Auto-generated method stub
		List<TeamSpaceMemberInfo> teamSpaceMemberList = new ArrayList<TeamSpaceMemberInfo>();
		try {
			Cursor cursor = this.getDb().rawQuery(
					"select * from " + TABLE_NAME, null);
			if (false == cursor.moveToFirst()) {
				return teamSpaceMemberList;
			}

			do {
				TeamSpaceMemberInfo teamSpaceInfo = new TeamSpaceMemberInfo();
				teamSpaceInfo.setUserId(cursor.getString(cursor
						.getColumnIndex(IBaseDao.user_id)));
				teamSpaceInfo.setMemberShipsId(cursor.getString(cursor
						.getColumnIndex(IBaseDao.memberships_id)));
				teamSpaceInfo.setTeamSpaceId(cursor.getString(cursor
						.getColumnIndex(IBaseDao.teamspace_id)));
				teamSpaceInfo.setType(cursor.getString(cursor
						.getColumnIndex(IBaseDao.type)));
				teamSpaceInfo.setName(cursor.getString(cursor
						.getColumnIndex(IBaseDao.name)));
				teamSpaceInfo.setLoginName(cursor.getString(cursor
						.getColumnIndex(IBaseDao.loginName)));
				teamSpaceInfo.setDescription(cursor.getString(cursor
						.getColumnIndex(IBaseDao.description)));
				teamSpaceInfo.setTeamRole(cursor.getString(cursor
						.getColumnIndex(IBaseDao.team_role)));
				teamSpaceInfo.setRole(cursor.getString(cursor
						.getColumnIndex(IBaseDao.role)));

				teamSpaceMemberList.add(teamSpaceInfo);
			} while (cursor.moveToNext());
		} catch (Exception e) {
			e.printStackTrace();
			LogUtil.e(LOG_TAG,
					"getTeamSpaceMemberList is fail:" + e.getLocalizedMessage());
		}
		return teamSpaceMemberList;
	}

	@Override
	public void deleteTeamSpaceMember(String teamSpaceId, String membershipsId) {
		// TODO Auto-generated method stub
		try {
			String where = teamspace_id + "=? and " + memberships_id + "=?";
			String[] whereValue = { teamSpaceId, membershipsId };
			this.getDb().delete(TABLE_NAME, where, whereValue);
		} catch (Exception e) {
			e.printStackTrace();
			LogUtil.e(LOG_TAG,
					"deleteTeamSpaceMember is fail:" + e.getLocalizedMessage());
		}
	}

	@Override
	public TeamSpaceMemberInfo getTeamSpaceMember(String teamSpaceId,
			String membershipsId) {
		// TODO Auto-generated method stub
		TeamSpaceMemberInfo teamSpaceMemberInfo = null;
		try {
			Cursor cursor = this.getDb().query(
					TABLE_NAME,
					new String[] { user_id, memberships_id, teamspace_id, type,
							name, loginName, description, team_role, role },
					teamspace_id + "=? and " + memberships_id + "=?",
					new String[] { teamSpaceId, membershipsId }, null, null, null);
			if (cursor.moveToFirst()) {
				teamSpaceMemberInfo = new TeamSpaceMemberInfo();
				
				teamSpaceMemberInfo.setUserId(cursor.getString(cursor
						.getColumnIndex(IBaseDao.user_id)));
				teamSpaceMemberInfo.setTeamSpaceId(cursor.getString(cursor
						.getColumnIndex(IBaseDao.teamspace_id)));
				teamSpaceMemberInfo.setMemberShipsId(cursor.getString(cursor
						.getColumnIndex(IBaseDao.memberships_id)));
				teamSpaceMemberInfo.setType(cursor.getString(cursor
						.getColumnIndex(IBaseDao.type)));
				teamSpaceMemberInfo.setName(cursor.getString(cursor
						.getColumnIndex(IBaseDao.name)));
				teamSpaceMemberInfo.setLoginName(cursor.getString(cursor
						.getColumnIndex(IBaseDao.loginName)));
				teamSpaceMemberInfo.setDescription(cursor.getString(cursor
						.getColumnIndex(IBaseDao.description)));
				teamSpaceMemberInfo.setTeamRole(cursor.getString(cursor
						.getColumnIndex(IBaseDao.team_role)));
				teamSpaceMemberInfo.setRole(cursor.getString(cursor
						.getColumnIndex(IBaseDao.role)));
			}

		} catch (Exception e) {
			e.printStackTrace();
			LogUtil.e(LOG_TAG,
					"getTeamSpace is fail:" + e.getLocalizedMessage());
		}
		return teamSpaceMemberInfo;
	}

	@Override
	public long getLastUpdateTime(String objectId) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public void updateLastUpdateTime(String objectId) {
		// TODO Auto-generated method stub

	}

}
